/*
 * Copyright 2002-2006 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.jwtools.tmt.ui;

import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;

import net.jwtools.tmt.domain.IVisit;

import org.springframework.binding.form.FieldMetadata;
import org.springframework.binding.validation.ValidationListener;
import org.springframework.binding.validation.ValidationResults;
import org.springframework.richclient.command.ActionCommand;
import org.springframework.richclient.form.AbstractForm;
import org.springframework.richclient.form.builder.TableFormBuilder;

/**
 * Form to handle the properties of a Contact object. It uses a
 * {@link TableFormBuilder} to construct the layout of the form. Contact object
 * properties are easily bound to UI controls using the form builder's
 * {@link TableFormBuilder#add(String)} method. The platform takes care of
 * determining which kind of control to create based on the type of the property
 * in question.
 * 
 * @author Larry Streepy
 */
public class TerritorySelectInfoForm extends AbstractForm {
	List<IVisit> visits;
	private JComboBox visitsComboBox;
	private JCheckBox finished;
	private Integer previousAbsent;
	private JComponent getVisitsComboBox() {
		return visitsComboBox;
	}


	public boolean isFinished() {
		return finished.isSelected();
	}


	public void setVisitsComboBox(JComboBox visitsComboBox) {
		this.visitsComboBox = visitsComboBox;
	}

	@Override
	public void setFormObject(Object formObject) {
        super.setFormObject(formObject);
        setPreviousAbsent(((IVisit)formObject).getNotHomeNumber());
    }
	
	public TerritorySelectInfoForm(IVisit visit) {
		super(visit);
		this.getFormModel().setId("territorySelectInfoForm");
		setPreviousAbsent(visit.getNotHomeNumber());
	}
	
	
	protected JComponent createFormControl() {
		TableFormBuilder formBuilder = new TableFormBuilder(getBindingFactory());
		formBuilder.setLabelAttributes("colGrId=label colSpec=right:pref");
		//PersonService personService = (PersonService) getApplicationServices()
				//.getService(PersonService.class);
		FieldMetadata md = getFormModel().getFieldMetadata( "checkoutDate" );
		md.setReadOnly( true );
		formBuilder.addSeparator("Select a Territory");
		if (visitsComboBox!=null) {
			formBuilder.row();
			formBuilder.getLayoutBuilder().cell(getComponentFactory().createLabel("territorySelectInfoForm.territory.label"));
			formBuilder.getLayoutBuilder().cell(getVisitsComboBox(),"colSpan=3");
		}
		formBuilder.row();
		formBuilder.add("checkoutDate", "colSpan=3");
		formBuilder.row();
		formBuilder.add("checkoutBy.displayName", "colSpan=3");
		formBuilder.row();
		formBuilder.add("notHomeNumber", "colSpan=3");
		formBuilder.row();
		formBuilder.add("checkinDate", "colSpan=3");
		formBuilder.row();
		
		final JButton checkinButton = (JButton) ((ActionCommand) getActiveWindow().getCommandManager().getCommand("checkinCommand")).createButton();
		//formBuilder.getLayoutBuilder().cell(getComponentFactory().createLabel("territorySelectInfoForm.finished.label"));
		
		
		formBuilder.getLayoutBuilder().cell(getComponentFactory().createLabel("territorySelectInfoForm.finished"));
		formBuilder.getLayoutBuilder().cell(finished = new JCheckBox());
		formBuilder.getLayoutBuilder().cell();
		formBuilder.getLayoutBuilder().cell(checkinButton);
		addValidationListener(new ValidationListener() {
			@Override
			public void validationResultsChanged(ValidationResults results) {
				checkinButton.setEnabled(!results.getHasErrors());
			}
		
		});
		JComponent form = formBuilder.getForm();
		return form;
	}

	/**
	 * Try to place the focus in the firstNameField whenever the initial focus
	 * is being set.
	 * 
	 * @return
	 */
	public boolean requestFocusInWindow() {
		return this.requestFocusInWindow();
	}


	public Integer getPreviousAbsent() {
		return previousAbsent;
	}


	public void setPreviousAbsent(Integer previousAbsent) {
		if (previousAbsent==null) previousAbsent=0;
		this.previousAbsent = previousAbsent;
	}
}